home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / etc / cron.daily / apt < prev    next >
Encoding:
Text File  |  2006-09-27  |  5.4 KB  |  203 lines

  1. #!/bin/sh
  2. #
  3.  
  4. #set -e
  5. #
  6. # This file understands the following apt configuration variables:
  7. #
  8. #  "APT::Periodic::Update-Package-Lists=1"
  9. #  - Do "apt-get update" automatically every n-days (0=disable)
  10. #    
  11. #  "APT::Periodic::Download-Upgradeable-Packages=0",
  12. #  - Do "apt-get upgrade --download-only" every n-days (0=disable)
  13. #  "APT::Periodic::AutocleanInterval"
  14. #  - Do "apt-get autoclean" every n-days (0=disable)
  15. #
  16. #  "APT::Periodic::Unattended-Upgrade"
  17. #  - Run the "unattended-upgrade" security upgrade script 
  18. #    every n-days (0=disabled)
  19. #    Requires the package "unattended-upgrades" and will write
  20. #    a log in /var/log/unattended-upgrades
  21. #  "APT::Archives::MaxAge",
  22. #  - Set maximum allowed age of a cache package file. If a cache 
  23. #    package file is older it is deleted (0=disable)
  24. #
  25. #  "APT::Archives::MaxSize",
  26. #  - Set maximum size of the cache in MB (0=disable). If the cache
  27. #    is bigger, cached package files are deleted until the size
  28. #    requirement is met (the biggest packages will be deleted 
  29. #    first).
  30. #
  31. #  "APT::Archives::MinAge"
  32. #  - Set minimum age of a package file. If a file is younger it
  33. #    will not be deleted (0=disable). Usefull to prevent races 
  34. #    and to keep backups of the packages for emergency.
  35.  
  36. check_stamp()
  37. {
  38.     stamp="$1"
  39.     interval="$2"
  40.  
  41.     if [ $interval -eq 0 ]; then
  42.         return 1
  43.     fi
  44.  
  45.     if [ ! -f $stamp ]; then
  46.         return 0
  47.     fi
  48.  
  49.     # compare midnight today to midnight the day the stamp was updated
  50.     stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
  51.     now=$(date --date=$(date --iso-8601) +%s)
  52.     delta=$(($now-$stamp))
  53.  
  54.     # intervall is in days,
  55.     interval=$(($interval*60*60*24))
  56.     #echo "stampfile: $1"
  57.     #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
  58.  
  59.     if [ $delta -ge $interval ]; then
  60.         return 0
  61.     fi
  62.  
  63.     return 1
  64. }
  65.  
  66. update_stamp()
  67. {
  68.     stamp="$1"
  69.  
  70.     touch $stamp
  71. }
  72.  
  73.  
  74.  
  75. # we check here if autoclean was enough sizewise
  76. check_size_constraints()
  77. {
  78.     # min-age in days
  79.     MaxAge=0
  80.     MinAge=2
  81.     MaxSize=0
  82.     CacheDir="var/cache/apt"
  83.     CacheArchive="archives/"
  84.     eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  85.     eval $(apt-config shell MinAge APT::Archives::MinAge)
  86.     eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  87.     eval $(apt-config shell Dir Dir)
  88.     eval $(apt-config shell CacheDir Dir::Cache)
  89.     eval $(apt-config shell CacheArchive Dir::Cache::archives)
  90.  
  91.     # sanity check
  92.     if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  93.     echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  94.     exit
  95.     fi
  96.     
  97.     Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  98.  
  99.     # check age
  100.     if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  101.     find $Cache -name "*.deb"  \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  102.     elif [ ! $MaxAge -eq 0 ]; then
  103.     find $Cache -name "*.deb"  -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  104.     fi
  105.     
  106.     # check size
  107.     if [ ! $MaxSize -eq 0 ]; then
  108.     # maxSize is in MB
  109.     MaxSize=$(($MaxSize*1024))
  110.  
  111.     #get current time
  112.     now=$(date --date=$(date --iso-8601) +%s)
  113.     MinAge=$(($MinAge*24*60*60))
  114.  
  115.     # reverse-sort by mtime
  116.     for file in $(ls -rt $Cache/*.deb 2>/dev/null); do 
  117.         du=$(du -s $Cache)
  118.         size=${du%%/*}
  119.         # check if the cache is small enough
  120.         if [ $size -lt $MaxSize ]; then
  121.         break
  122.         fi
  123.  
  124.         # check for MinAge of the file
  125.         if [ ! $MinAge -eq 0 ]; then 
  126.         # check both ctime and mtime 
  127.         mtime=$(stat -c %Y $file)
  128.         ctime=$(stat -c %Z $file)
  129.         if [ $mtime -gt $ctime ]; then
  130.             delta=$(($now-$mtime))
  131.         else
  132.             delta=$(($now-$ctime))
  133.         fi
  134.         #echo "$file ($delta), $MinAge"
  135.         if [ $delta -le $MinAge ]; then
  136.             #echo "Skiping $file (delta=$delta)"
  137.             break
  138.         fi
  139.         fi
  140.  
  141.         # delete oldest file
  142.         rm -f $file
  143.     done
  144.     fi
  145. }
  146.  
  147.  
  148. UpdateInterval=0
  149. DownloadUpgradeableInterval=0
  150. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  151. AutocleanInterval=$DownloadUpgradeableInterval
  152. eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
  153.  
  154. UnattendedUpgradeInterval=0
  155. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  156.  
  157.  
  158. # laptop check, on_ac_power returns:
  159. #       0 (true)    System is on mains power
  160. #       1 (false)   System is not on mains power
  161. #       255 (false) Power status could not be determined
  162. # Desktop systems always return 255 it seems
  163. if which on_ac_power >/dev/null; then
  164.     on_ac_power
  165.     if [ $? -eq 1 ]; then
  166.     exit 0
  167.     fi
  168. fi
  169.  
  170. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  171. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  172.     if apt-get -qq update 2>/dev/null; then
  173.     if which dbus-send >/dev/null; then
  174.         dbus-send --system / app.apt.dbus.updated boolean:true
  175.     fi
  176.         update_stamp $UPDATE_STAMP
  177.     fi
  178. fi
  179.  
  180. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  181. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  182.     apt-get -qq -d dist-upgrade 2>/dev/null
  183.     update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  184. fi
  185.  
  186. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  187. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  188.     apt-get -qq autoclean
  189.     update_stamp $AUTOCLEAN_STAMP
  190. fi
  191.  
  192. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  193. if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  194.     unattended-upgrade
  195.     update_stamp $UPGRADE_STAMP
  196. fi
  197.  
  198. # check cache size 
  199. check_size_constraints
  200.